//
//  SDFamousDoctorsView.m
//  SuperDoctor
//
//  Created by 李忠 on 7/8/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDFamousDoctorsView.h"
#import "SDFakeButton.h"
#import <objc/message.h>

#define kPading (10*_sdScale)
@interface SDFamousDoctorsView ()

//医生名字
@property (nonatomic, strong) UILabel *lb_userInfo;
//科室
@property (nonatomic, strong) UILabel *lb_type;
//显示到的相同专业的医生
@property (nonatomic, strong) UILabel *lb_mateDoctors;
//显示名医头像
@property (nonatomic, strong) UIImageView *iv_famosHeader;
//按钮
@property (nonatomic, strong) UIButton *btn_apply;

//存储SDFateArray 创建的方法
@property (nonatomic, strong) NSArray *fateArray;

//缩放比例
@property (nonatomic, assign) CGFloat sdScale;

@end

@implementation SDFamousDoctorsView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
        [self checkDevice];
        [self setupUI];
        [self setupLayout];
    
    }
    return self;
}

- (void)checkDevice{
    if (kDevice_Is_iPhone5) {
        _sdScale = 0.8;
    }else if (kDevice_Is_iPhone6){
        _sdScale = 1;
    }else if (kDevice_Is_iPhone6Plus){
        _sdScale = 1;
    }else{
        _sdScale = 0.7;
    }
}

- (void)setupUI{
   
    self.backgroundColor = [UIColor clearColor];
    
    //为SDFateButton 创建手势
   
    //MARK: 细分
    SDFakeButton *subdivide = [self newNorButtonNotice:@"请选择疾病细分"];
    [subdivide addTarget:self action:@selector(clickFate:)];

    
    //并发症
    SDFakeButton *syndrome = [self newNorButtonNotice:@"请选择并发症(可多选)"];
    [syndrome addTarget:self action:@selector(clickFate:)];
    
    //是否确认
    SDFakeButton *verify = [self newSwitchWithNotice:@"是否确认" firstTitle:@"已确认" secondTitle:@"症状疑似" forSelect:YES];
    //是否接受过治疗
    SDFakeButton *cure = [self newSwitchWithNotice:@"是否接受过治疗" firstTitle:@"接受过" secondTitle:@"未接受" forSelect:NO];
    //请选择治疗方式
    SDFakeButton *cureWay = [self newNorButtonNotice:@"请选择治疗方式"];
    [cureWay addTarget:self action:@selector(clickFate:)];
    
    
    cureWay.hidden = YES;
    [self addSubview:subdivide];
    [self addSubview:syndrome];
    [self addSubview:verify];
    [self addSubview:cure];
    [self addSubview:cureWay];
    self.fateArray = @[ subdivide, syndrome, verify,cure,cureWay ];
    //MARK: 赋值block方法
    cure.hiddenFateBlock = ^(BOOL hidden){
        cureWay.hidden = hidden;
    };
}

#pragma mark - 这里是个手势，我怎样在SDFateButton中拆除view 转换到这里
- (void)clickFate:(SDFakeButton *)obj{
    
    //MARK: 取得被点击SDFateButton的index
    NSInteger index = [self.fateArray indexOfObject:obj];
    
    switch (index) {
        case 0:
            if (_pushOtherBlock) {
                _pushOtherBlock(SDPatientSubdivide, @"疾病细分");
            }
            break;
        case 1:
            if (_pushOtherBlock) {
                _pushOtherBlock(SDPatientSyndrome, @"并发症");
            }
            break;
        case 4:
            if (_pushOtherBlock) {
                _pushOtherBlock(SDPatientSureWay, @"治疗方式");
            }
            break;
        default:
            break;
    }
    
}

#pragma mark
#pragma mark - 添加约束
- (void)setupLayout{
    
    //MARK: 名字约束
    [self.lb_userInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kPading * 2);
        make.leading.mas_equalTo(kPading);
        make.trailing.mas_equalTo(-kPading);
        make.height.mas_equalTo(20);
    }];
    //MARK: 科室约束
    [self.lb_type mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lb_userInfo.mas_bottom).offset(kPading);
        make.leading.mas_equalTo(self.lb_userInfo.mas_leading);
        make.height.mas_equalTo(20);
    }];
    
    //MARK:fateArray 约束
    [self.fateArray enumerateObjectsUsingBlock:^(SDFakeButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (idx == 0) {
            [obj mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(self.lb_type.mas_bottom).mas_offset(kPading);
                make.leading.mas_offset(kPading);
                make.trailing.mas_offset(-kPading);
            }];
        }else if (idx == self.fateArray.count - 1){
            //因为最后一个需要在最后设置底部约束，所以我就要单独设置
            [self layoutFateArray:obj index:(idx-1) forState:YES];
        }else{
            
            [self layoutFateArray:obj index:(idx-1) forState:NO];
        }
    }];
    
    //名医列表约束
    [self.lb_mateDoctors mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.mas_offset(kPading);
        make.trailing.mas_offset(-kPading);
        make.height.mas_offset(kPading * 2);
        make.bottom.mas_equalTo(self.iv_famosHeader.mas_top).mas_offset(-kPading * 2);
    }];

    [self.iv_famosHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(self.btn_apply.mas_top).mas_offset(-kPading * 2);
        make.height.width.mas_offset(kPading * 9);
    }];
    [self.btn_apply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_offset(kPading);
        make.trailing.mas_offset(-kPading);
        make.bottom.mas_offset(-kPading);
        make.height.mas_offset(kPading * 4);
    }];
}

#pragma mark
#pragma mark - 抽出fateArray数组中的相同的约束设置方式
- (void)layoutFateArray:(SDFakeButton *)obj index:(NSInteger)index forState:(BOOL)flag{
    
    SDFakeButton *condemned = (SDFakeButton *)self.fateArray[index];
    SDFakeButton *comparison = (SDFakeButton *)self.fateArray[0];
    
    [obj mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(condemned.mas_bottom).mas_offset(kPading);
        make.leading.mas_equalTo(kPading);
        make.trailing.mas_equalTo(-kPading);
        make.height.mas_equalTo(comparison);
//        if (flag)make.centerY.mas_equalTo(self.mas_centerY);
        if (flag)make.bottom.mas_equalTo(self.lb_mateDoctors.mas_top).mas_offset(-2 * kPading);
            
    }];
    

}

#pragma mark
#pragma mark - 创建SDFakeButton
- (SDFakeButton *)newNorButtonNotice:(NSString *)notice{
    SDFakeButton *temp = [[SDFakeButton alloc] initWithType:SDFakeButtondefault];
    [temp setImage:[UIImage imageNamed:@"illness_button_input_blue.png"]];
    [temp setNoticeTitle:notice withColor:[UIColor lightGrayColor]];
    return temp;
}
- (SDFakeButton *)newSwitchWithNotice:(NSString *)notice firstTitle:(NSString *)firtTitle secondTitle:(NSString *)secondTitle forSelect:(BOOL)flag{
    
    SDFakeButton *temp = [[SDFakeButton alloc] initWithType:SDFakeButtonSelected];
    [temp setNoticeTitle:notice withColor:[UIColor lightGrayColor]];
    [temp setFirstTitle:firtTitle withSecondTitle:secondTitle forState:UIControlStateNormal];
    [temp setSwitchBtnTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [temp setNorImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] withSelectedImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
    [temp setDefaultButton:flag];
    
    return temp;
}

#pragma mark
#pragma mark - 懒加载View
- (UILabel *)lb_userInfo{
    if (_lb_userInfo == nil) {
        _lb_userInfo = [[UILabel alloc] init];
        _lb_userInfo.text = @"用户信息:";
//        _lb_userInfo.font = FONT(17*_sdScale);
        _lb_userInfo.font = [UIFont systemFontOfSize:17*_sdScale];
        [self addSubview:_lb_userInfo];
    }
    return  _lb_userInfo;
}
- (UILabel *)lb_type{
    if (_lb_type == nil) {
        _lb_type = [[UILabel alloc] init];
        _lb_type.text = @"疾病类型:";
//        _lb_type.font = FONT(17*_sdScale);
        _lb_type.font = [UIFont systemFontOfSize:17*_sdScale];
        [self addSubview:_lb_type];
    }
    return _lb_type;
}
- (UILabel *)lb_mateDoctors{
    if (_lb_mateDoctors == nil) {
        _lb_mateDoctors = [[UILabel alloc] init];
        _lb_mateDoctors.text = @"为你匹配到: 0位医生";
        _lb_mateDoctors.textAlignment = NSTextAlignmentCenter;
        _lb_mateDoctors.font = [UIFont systemFontOfSize:17*_sdScale];
        [self addSubview:_lb_mateDoctors];
    }
    return _lb_mateDoctors;
}

- (UIImageView *)iv_famosHeader{
    if (_iv_famosHeader == nil) {
        _iv_famosHeader = [[UIImageView alloc] init];
        _iv_famosHeader.image = [UIImage imageNamed:@"illness_img_person.png"];
        [self addSubview:_iv_famosHeader];
    }
    return _iv_famosHeader;
}
- (UIButton *)btn_apply{
    if (_btn_apply == nil) {
        
        _btn_apply = [[UIButton alloc] init];
        [_btn_apply setTitle:@"就医申请" forState:UIControlStateNormal];
        [_btn_apply setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_apply.backgroundColor = kThemeColor;
        _btn_apply.titleLabel.font = [UIFont systemFontOfSize:17*_sdScale];
        [_btn_apply addTarget:self action:@selector(clickDoctor) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btn_apply];
    }
    return _btn_apply;
}
#pragma mark
#pragma mark - 实现接口方法
- (void)setUserName:(NSString *)userName withTypeName:(NSString *)typeName withDoctors:(NSInteger)num{
    
    self.lb_userInfo.text = [NSString stringWithFormat:@"用户信息:  %@", userName];
    self.lb_type.text = [NSString stringWithFormat:@"疾病类型:  %@", typeName];
    self.lb_mateDoctors.text = [NSString stringWithFormat:@"为你匹配到: %zd位医生", num];
    
}

#pragma mark
#pragma mark - 实现接口
- (void)setDiseaseType:(NSString *)disease forStyle:(SDPatientType)patientType{
    SDFakeButton *fakeButton = self.fateArray[patientType];
    [fakeButton setNoticeTitle:disease withColor:[UIColor blackColor]];
}

- (void)clickDoctor{
    if (_pushFocusDoctorBlock) {
        _pushFocusDoctorBlock();
    }
}




@end
